package com.android.fragmentdemo.defineviews
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.util.Log
import android.widget.RadioGroup
import com.android.fragmentdemo.R

class BottomBackgroundRadioGroup @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : RadioGroup(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_E60C3F)
        }
    }

    private val mPath by lazy {
        Path()
    }

    init {
        setBackgroundColor(context.resources.getColor(R.color.color_00000000))
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d(TAG, "$TAG---override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)")
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val cx = measuredWidth.toFloat()
        val cy = measuredHeight.toFloat()

        Log.d(TAG, "$TAG---cx:${cx},cy:${cy}")

        canvas.apply {
            mPath.apply {
                moveTo(0F, cy / 10)
                lineTo(cx * 10 / 25, cy / 10)
                quadTo(
                    cx / 2,
                    -cy / 10,
                    cx * 15 / 25,
                    cy / 10
                )
                lineTo(cx, cy / 10)
                lineTo(cx, cy)
                lineTo(0F, cy)
                close()
            }

            drawPath(mPath, mPaint)
        }
    }

    companion object {
        const val TAG = "BottomBackgroundRadioGroup"
    }
}